package com.example.knowman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.knowman.ObService.OBKnowMainService;
import com.example.knowman.ObService.OBlabelCategoryService;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.dao.knowCategoryLevels;
import com.example.knowman.dao.knowCategoryLevelsSingle;
import com.example.knowman.dao.knowCategoryWithStaffname;
import com.example.knowman.entity.knowCategory;
import com.example.knowman.entity.staffList;
import com.example.knowman.mapper.KnowCategoryMapper;
import com.example.knowman.mapper.KnowMainMapper;
import com.example.knowman.mapper.LabelCategoryMapper;
import com.example.knowman.mapper.StaffListMapper;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.IKnowMainService;
import com.example.knowman.service.IStaffListService;
import com.example.knowman.service.IlabelCategoryService;
import org.springframework.stereotype.Service;

//a复制给b

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class KnowCategoryServiceImpl extends ServiceImpl<KnowCategoryMapper, knowCategory> implements IKnowCategoryService {

    private final KnowCategoryMapper knowCategoryMapper;

    private final IStaffListService iStaffListService;

    private final OBKnowMainService obKnowMainService;

    private final OBlabelCategoryService oblabelCategoryService;


    private final StaffListMapper staffListMapper;
    public KnowCategoryServiceImpl(KnowCategoryMapper knowCategoryMapper,IStaffListService iStaffListService,StaffListMapper staffListMapper,OBKnowMainService obKnowMainService,OBlabelCategoryService oblabelCategoryService) {
//        this.iTable1Service = iTable1Service;
        this.knowCategoryMapper = knowCategoryMapper;
        this.iStaffListService = iStaffListService;
        this.staffListMapper = staffListMapper;
        this.obKnowMainService = obKnowMainService;
        this.oblabelCategoryService = oblabelCategoryService;
    }


    @Override
    public List<knowCategory> getLittleClass(Integer parentId) {
        List<knowCategory> list =  this.lambdaQuery().select().eq(knowCategory::getParentId,parentId).list();
        return list;
    }

    @Override
    public Message deleteByKnowCateId(Integer KnowCateId) {
        Message message = new Message();
        //判断是否还有子类存在
        Integer childNum = this.lambdaQuery()
                .select()
                .eq(knowCategory::getParentId,KnowCateId)
                .count();
        Integer knowMainContainNum = obKnowMainService.returnContainKnowCateIdCount(KnowCateId);
        System.out.println(knowMainContainNum);
        Integer labelCategoryContainNum = oblabelCategoryService.returnContainKnowCateIdCount(KnowCateId);
        System.out.println(labelCategoryContainNum);
        String tips =new String();
        if (childNum>0){
            tips =tips + "该类下还存在子类|";
        }
        if (knowMainContainNum>0){
            tips =tips + "该类在knowMain(发布的知识表)中还存在数据|";
        }
        if (labelCategoryContainNum>0){
            tips =tips + "该类在labelCategory(标签表)中还存在数据|";
        }

        if(childNum>0 || knowMainContainNum>0 || labelCategoryContainNum>0){
            message.setCode(100);
            message.setTip(tips);
            return message;
        }



        QueryWrapper<knowCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("\"knowCateId\"",KnowCateId);
        try{
            this.remove(wrapper);
            message.setCode(200);
            message.setTip("删除成功");
        }catch (Exception e){
            message.setCode(100);
            message.setTip("删除失败");
        }

        return message;
    }



    @Override
    public Message update(Integer KnowCateId, String knowCateName, String notes) {
        Message message = new Message();
        this.lambdaUpdate().eq(knowCategory::getKnowCateId,KnowCateId)
                .set(knowCateName != null && !knowCateName.isEmpty(),knowCategory::getKnowCateName,knowCateName)
                .set(notes != null && !notes.isEmpty(),knowCategory::getNotes,notes)
                .update();
        message.setCode(200);
        message.setTip("修改成功");

        return message;
    }

    @Override
    public Message Insert(String knowCateName, Integer parentId, String notes, String staffCode) {
        knowCategory findMax = this.lambdaQuery().select()
                .eq(parentId != null,knowCategory::getParentId,parentId)
                .eq(parentId == null,knowCategory::getParentId,0)
                .orderByDesc(knowCategory::getCateId)
                .last("limit 1")
                .one();

        String maxCateId = new String();
        if(findMax == null){
            if(parentId != null){
                knowCategory find= this.selectByknowCateId(parentId);
                maxCateId = find.getCateId() + "00";
            }
            else {
                maxCateId = "00";
            }

        }
        else {
            maxCateId = findMax.getCateId();
        }

//        System.out.println(maxCateId);
        int maxId = Integer.parseInt(maxCateId);
        maxId++;
        String nextCateId = String.format("%0" + maxCateId.length() + "d", maxId);
//        System.out.println(nextCateId);


        Message message = new Message();
        knowCategory temp = new knowCategory();
        temp.setCateId(nextCateId);
        temp.setKnowCateName(knowCateName);
        temp.setParentId(parentId);
        temp.setNotes(notes);
        temp.setStaffCode(staffCode);

        try{
            this.save(temp);
            message.setCode(200);
            message.setTip("新增成功");
        }catch (Exception e){
            message.setCode(100);
            message.setTip("新增失败");
        }

        return message;
    }

//    @Override
//    public Message Insert(String cateId, String knowCateName, Integer parentId, String notes, String staffCode) {
//        Message message = new Message();
//
//
//
//        knowCategory temp = new knowCategory();
//        temp.setCateId(cateId);
//        temp.setKnowCateName(knowCateName);
//        temp.setParentId(parentId);
//        temp.setNotes(notes);
//        temp.setStaffCode(staffCode);
//
//        try{
//            this.save(temp);
//            message.setCode(200);
//            message.setTip("新增成功");
//        }catch (Exception e){
//            message.setCode(100);
//            message.setTip("新增失败");
//        }
//
//        return message;
//
//    }

    @Override
    public knowCategory selectByknowCateId(Integer knowCateId) {

        return this.lambdaQuery().select().eq(knowCategory::getKnowCateId,knowCateId).one();
    }

    @Override
    public List<knowCategory> showAll() {
        List<knowCategory> res =this.lambdaQuery().select().list();
        return res;
    }

    @Override
    public List<knowCategory> listByKnowCateName(String knowCateName) {
        List<knowCategory> res = new ArrayList<>();
        if(knowCateName != null && !knowCateName.isEmpty()){
            List<knowCategory> temp_one = this.lambdaQuery()
                    .select()
                    .eq(knowCateName != null && !knowCateName.isEmpty(),knowCategory::getKnowCateName,knowCateName).list();
            res.addAll(temp_one);
            List<knowCategory> temp_second = new ArrayList<>();
            for (knowCategory item : temp_one){
                List<knowCategory> temp = this.lambdaQuery().select().eq(knowCategory::getParentId,item.getKnowCateId()).list();
                for (knowCategory item2 : temp){
                    List<knowCategory> temp2 = this.lambdaQuery().select().eq(knowCategory::getParentId,item2.getKnowCateId()).list();
                    res.addAll(temp2);
                }
                res.addAll(temp);
            }
        }
        else {
            res=this.lambdaQuery().select().list();
        }
        return res;
    }

    @Override
    public IPage<knowCategory> getEasyClass(Integer pageNum, Integer pageSize, String knowCateName) {
        IPage<knowCategory> page = new Page<>(pageNum, pageSize);
        return this.lambdaQuery()
                .select()
                .like(knowCateName !=null && !knowCateName.isEmpty(),knowCategory::getKnowCateName,knowCateName)
                .page(page);

    }

    @Override
    public IPage<knowCategory> getFirstClass(Integer pageNum, Integer pageSize, String knowCateName) {
        IPage<knowCategory> page = new Page<>(pageNum, pageSize);
        return this.lambdaQuery()
                .select()
                .like(knowCateName !=null && !knowCateName.isEmpty(),knowCategory::getKnowCateName,knowCateName)
                .eq(knowCategory::getParentId,0)
                .page(page);
    }

    @Override
    public Map<Integer, String> getIdAndName() {
        return this.lambdaQuery().select().list().stream()
                .collect(Collectors.toMap(knowCategory::getKnowCateId, knowCategory::getKnowCateName));
    }

    @Override
    public knowCategoryWithStaffname getSingleById(Integer knowCateId) {

        knowCategoryWithStaffname res = new knowCategoryWithStaffname();
        knowCategory temp = this.lambdaQuery().select().eq(knowCategory::getKnowCateId,knowCateId).one();

        String staffName = iStaffListService.getStaffName(temp.getStaffCode());

        res.setParentId(temp.getParentId());
        res.setCateId(temp.getCateId());
        res.setNotes(temp.getNotes());
        res.setParentId(temp.getParentId());
        res.setStaffCode(temp.getStaffCode());
        res.setStaffName(staffName);
        res.setCreatetime(temp.getCreatetime());
        res.setKnowCateId(temp.getKnowCateId());
        res.setKnowCateName(temp.getKnowCateName());

        return res;
    }

    @Override
    public List<knowCategoryLevels> getAllLevels() {

        List<knowCategory> all = this.showAll();
        List<knowCategoryLevels> result = new ArrayList<>();

        for(knowCategory item:all){
            if(item.getParentId() == 0){
                knowCategoryLevels Sitem = new knowCategoryLevels();
                Sitem.setValue(item.getKnowCateId());
                Sitem.setLabel(item.getKnowCateName());
                List<knowCategoryLevels> SitemChildrens = new ArrayList<>();
                //for
                for(knowCategory item2:all){
                    if(item2.getParentId() == item.getKnowCateId()){
                        knowCategoryLevels Sitem2 = new knowCategoryLevels();
                        Sitem2.setValue(item2.getKnowCateId());
                        Sitem2.setLabel(item2.getKnowCateName());
                        List<knowCategoryLevels> Sitem2Childrens = new ArrayList<>();
                        //for
                        for(knowCategory item3:all){
                            if(item3.getParentId() == item2.getKnowCateId()){
                                knowCategoryLevels Sitem3 = new knowCategoryLevels();
                                Sitem3.setValue(item3.getKnowCateId());
                                Sitem3.setLabel(item3.getKnowCateName());

                                Sitem2Childrens.add(Sitem3);
                            }
                        }
                        Sitem2.setChildren(Sitem2Childrens);
                        SitemChildrens.add(Sitem2);
                    }
                }
                Sitem.setChildren(SitemChildrens);
                result.add(Sitem);
            }
        }


        return result;
    }

    @Override
    public List<knowCategoryLevels> getSingleLevels(Integer knowCateId) {
        //List<knowCategory> all = this.showAll();
        Map<Integer,knowCategory> MapAll = this.lambdaQuery().select().list().stream().collect(Collectors.toMap(knowCategory::getKnowCateId, Function.identity()));
//        knowCategoryLevelsSingle result = new knowCategoryLevelsSingle();

        List<knowCategoryLevels> result = new ArrayList<>();

        Integer level =1;
        knowCategory item3 = MapAll.get(knowCateId);
        knowCategory item2 = new knowCategory();
        knowCategory item = new knowCategory();
        if(item3.getParentId()!=0){
            item2 = MapAll.get(item3.getParentId());
            level++;
            if (item2.getParentId()!=0){
                item = MapAll.get(item2.getParentId());
                level++;
            }
        }

        //
        if(level == 3){
            knowCategoryLevels temp = new knowCategoryLevels();
            knowCategoryLevels temp2 = new knowCategoryLevels();
            knowCategoryLevels temp3 = new knowCategoryLevels();

            temp.setValue(item.getKnowCateId());
            temp.setLabel(item.getKnowCateName());
            temp2.setValue(item2.getKnowCateId());
            temp2.setLabel(item2.getKnowCateName());
            temp3.setValue(item3.getKnowCateId());
            temp3.setLabel(item3.getKnowCateName());

            List<knowCategoryLevels> t2Child = new ArrayList<>();
            List<knowCategoryLevels> t1Child = new ArrayList<>();

            t2Child.add(temp3);
            temp2.setChildren(t2Child);
            t1Child.add(temp2);
            temp.setChildren(t1Child);
            result.add(temp);

        }
        else{
            if(level ==2){
                knowCategoryLevels temp2 = new knowCategoryLevels();
                knowCategoryLevels temp3 = new knowCategoryLevels();

                temp2.setValue(item2.getKnowCateId());
                temp2.setLabel(item2.getKnowCateName());
                temp3.setValue(item3.getKnowCateId());
                temp3.setLabel(item3.getKnowCateName());

                List<knowCategoryLevels> t2Child = new ArrayList<>();

                t2Child.add(temp3);
                temp2.setChildren(t2Child);

                result.add(temp2);

            }
            else{
                knowCategoryLevels temp3 = new knowCategoryLevels();
                temp3.setValue(item3.getKnowCateId());
                temp3.setLabel(item3.getKnowCateName());

                result.add(temp3);
            }
        }

        return result;
    }

//    @Override
//    public Integer returnChildCount(Integer knowCateId) {
//
//        return this.lambdaQuery()
//                .select()
//                .eq(knowCategory::getParentId,knowCateId)
//                .count();
//    }

//    @Override
//    public List<knowCategory> getEasyClass(String knowCateName) {
//        List<knowCategory> list =  this.lambdaQuery()
//                .select()
//                .like(knowCateName !=null && !knowCateName.isEmpty(),knowCategory::getKnowCateName,knowCateName)
//                .list();
//        return list;
//    }


}
